﻿Namespace EIBData.DPT
    Public MustInherit Class EIBDataDPT_3_Base
        Inherits EIBDataDPT_Base

        Protected _directionBase As Boolean = Nothing
        Protected _stepCode As Byte = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data
            _directionBase = (data(0) And &H8) >> 3
            _stepCode = data(0) And &H7
        End Sub

        Sub New(direction As Boolean, stepCode As Byte)
            If stepCode > 7 Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _directionBase = direction
            _stepCode = stepCode
            MyBase._data = New Byte() {(_directionBase << 3) + (_stepCode And &H7)}
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = DPT_OctetLength
        End Function

        Public Overrides Function ToString() As String
            Return String.Format("Control: {0}, StepCode: {1}", _directionBase, _stepCode)
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_directionBase, _stepCode}
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(Boolean), GetType(Byte)}
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_BaseName As String
            Get
                Return "B1U3"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_OctetLength As Byte
            Get
                Return 1
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Name As String
            Get
                Return "DPT_3"
            End Get
        End Property

        Public Shared Shadows ReadOnly Property DPT_Number As String
            Get
                Return "3.xxx"
            End Get
        End Property
    End Class
End Namespace